package cc.openkit.admin.controller.api;

import cc.openkit.admin.model.CompanyInfo;
import cc.openkit.admin.model.Project;
import cc.openkit.admin.service.company.CompanyService;
import cc.openkit.admin.service.companyInfo.CompanyInfoService;
import cc.openkit.admin.service.houseRenovationOffer.HouseRenovationOfferService;
import cc.openkit.admin.service.project.ProjectService;
import cc.openkit.admin.service.shopType.ShopTypeService;
import cc.openkit.admin.service.task.TaskService;
import cc.openkit.admin.util.AppUtil;
import cc.openkit.admin.util.Pager;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.admin.vo.CompanyVo;
import cc.openkit.admin.vo.GongZhangVo;
import cc.openkit.admin.vo.ProjectVo;
import cc.openkit.admin.vo.ShiGongVo;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营中心 项目管理
 */
@Controller
@Scope("prototype")
@RequestMapping("/tcpmc")
public class TaskCenterProjectManagerController {

    private Logger log = Logger.getLogger(TaskController.class);
    private Map<String,Object> map = new HashMap<String, Object>();
    @Resource
    private TaskService taskService;
    @Resource
    private HouseRenovationOfferService houseRenovationOfferService;
    @Resource
    private ShopTypeService shopTypeService;
    @Resource
    private ProjectService projectService;
    @Resource
    private CompanyInfoService companyInfoService;
    @Resource
    private CompanyService companyService;

    /**
     * 运营中心 》》项目管理 》》进行中项目列表 筛选（判断projectid要不为null）
     * @return
     */
    @RequestMapping(value = "/centerGoing", method = RequestMethod.POST)
    @ResponseBody
    public Object centerGoing(HttpServletRequest request) throws Exception{
        log.info("centerGoing");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }

        //获取登录人id
        String uid = request.getHeader("uid");
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数

        String limit = request.getParameter("limit");// 获得每页显示条数

        //筛选条件
        String cityId = request.getParameter("cityId");//城市id
        String proStatus = request.getParameter("proStatus");//项目状态 1.等待相关人员接单 2.已全部接单，等待上门 3.进行中
        String proCurrentStage = request.getParameter("proCurrentStage");//当前阶段名称
        String minDate = request.getParameter("minDate");//立项最小时间
        String maxDate = request.getParameter("maxDate");//建立项目最大时间

        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }
        Project p = new Project();
        p.setProStatus(4);

        if ((page != null && !"".equals(page)) && (limit != null && !"".equals(limit))) {
            p.setPager(new Pager(Integer.parseInt(page), Integer.parseInt(limit)));
        }

        if (proStatus!=null && !"".equals(proStatus)){
            p.setProStatus(Integer.parseInt(proStatus));
        }
        if (proCurrentStage!=null && !"".equals(proCurrentStage)){
            p.setProCurrentStage(proCurrentStage);
        }
        if (minDate!=null && !"".equals(minDate)){
            p.setMinDate(minDate);
        }
        if (maxDate!=null && !"".equals(maxDate)){
            p.setMaxDate(maxDate);
        }
        if (cityId!=null && !"".equals(cityId)){
            p.setCityId(cityId);
        }

        List<ProjectVo> projectVoList=projectService.centerGoing(p);


        map.put("code",200);
        map.put("projectVoList",projectVoList);
        return JSONObject.toJSON(map);
    }

    /**
     * 运营中心 》》项目管理 》》 已完成 项目列表 筛选
     * @return
     */
    @RequestMapping(value = "/centerFinish", method = RequestMethod.POST)
    @ResponseBody
    public Object centerFinish(HttpServletRequest request) throws Exception{
        log.info("centerFinish");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }

        //获取登录人id
        String uid = request.getHeader("uid");
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数

        String limit = request.getParameter("limit");// 获得每页显示条数

        //筛选条件
        String cityId = request.getParameter("cityId");//城市id
        String minDate = request.getParameter("minDate");//立项最小时间
        String maxDate = request.getParameter("maxDate");//建立项目最大时间



        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }
        Project p = new Project();
        p.setProStatus(4);

        if ((page != null && !"".equals(page)) && (limit != null && !"".equals(limit))) {
            p.setPager(new Pager(Integer.parseInt(page), Integer.parseInt(limit)));
        }

        if (minDate!=null && !"".equals(minDate)){
            p.setMinDate(minDate);
        }
        if (maxDate!=null && !"".equals(maxDate)){
            p.setMaxDate(maxDate);
        }
        if (cityId!=null && !"".equals(cityId)){
            p.setCityId(cityId);
        }

        List<ProjectVo> projectVoList=projectService.centerFinish(p);


        map.put("code",200);
        map.put("projectVoList",projectVoList);
        return JSONObject.toJSON(map);
    }

    /**
     * 运营中心 》》项目管理 》》 查看项目详情1
     * @return
     */
    @RequestMapping(value = "/centerFindProjectByProId", method = RequestMethod.POST)
    @ResponseBody
    public Object centerFindProjectByProId(HttpServletRequest request) throws Exception{
        log.info("centerFindProjectByProId");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isCoLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        //获取登录人id
        String uid = request.getHeader("uid");

        String proId = request.getParameter("proId");//项目id

        isNUllMap.put(uid,"用户id");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return new Gson().toJson(map);
        }

        //项目的基本信息
        ProjectVo projectVo=projectService.findProjectByProId(proId);

        //组建团队（包含施工方，设计方（里面包含设计师），监理，工长）

        //1.根据project_id 查询 施工方
        List<ShiGongVo> shiGongVos=taskService.findShiGong(proId);

        //2.根据project_id 和 task_type 查询 工长信息
        //task_type 1.属于运营中心的任务 2.装修公司 3.工长 4.监理 5.设计方（设计师的公司） 6.设计师
        List<GongZhangVo> gongZhangVos=taskService.findGongZhang(proId);


        //3.根据project_id 查询 设计方

        //4.根据project_id 查询 设计师



        map.put("code",200);
        map.put("projectVo",projectVo);
        return JSONObject.toJSON(map);
    }

}
